package com.platform.os.util;

import java.io.File;

/**
 * @author KangJun 2007-10-07.
 */
public class FileInfo {
	//String fullfileName;	全文件名
	//String fileName;		不带扩展名的文件名
	//String fileExtend;	文件扩展名
	//String path;			系统路径
	//String urlPath;		以项目根目录为起点的路径
	//String filePath;		path + fullfileName
	//String urlFilePath;	urlPath + fullfileName

	/** 获得文件大小.
	 * @param filePath
	 * @return 0表示文件不存在或文件未目录
	 */
	public static long getFileSize(String filePath) {
		long size = 0;
		File file = new File(filePath);
		if (file.exists()) {
			if (!file.isDirectory()) {
				size = file.length();
			}
		}

		return size;
	}

	/** 获得去掉后缀的文件名.
	 * @param fullfileName
	 * @return String
	 */
	public static String getFileName(String fullFileName) {
		if (fullFileName == null) {
			return null;
		}
		int index = fullFileName.lastIndexOf(".");
		if (index == -1) {
			return fullFileName;
		} else {
			return fullFileName.substring(0, index);
		}
	}

	/** 获得文件扩展名.
	 * @param fullfileName
	 * @return String
	 */
	public static String getFileExtend(String fullFileName) {
		if (fullFileName == null) {
			return null;
		}
		int index = fullFileName.lastIndexOf(".");
		if (index == -1) {
			return null;
		} else {
			return fullFileName.substring(index + 1);
		}
	}

	/** 获得全文件名.
	 * @param fullfileName
	 * @return String
	 */
	public static String getFullFileName(String fileName, String fileExtend) {
		if (fileName == null) {
			return null;
		}
		if (fileExtend == null || fileExtend.equals("")) {
			return fileName;
		} else {
			return fileName + "." + fileExtend;
		}
	}

	/** 获得全路径文件名.
	 * @param urlPath
	 * @param fullfileName
	 * @return String
	 */
	public static String getFilePath(String urlPath, String fullFileName) {
		if (fullFileName == null) {
			return null;
		}
		String basePath = ApplicationInitial.getApplactionPath();
		if (basePath.indexOf("\\") == -1) {
			return basePath + urlPath + "/" + fullFileName;
		} else {
			return basePath + urlPath.substring(1).replace('/', '\\') + "\\" + fullFileName;
		}
	}

	/** 获得全路径文件名.
	 * @param urlFilePath
	 * @return String
	 */
	public static String getFilePath(String urlFilePath) {
		String basePath = ApplicationInitial.getApplactionPath();
		if (basePath.indexOf("\\") == -1) {
			return basePath + urlFilePath;
		} else {
			return basePath + urlFilePath.substring(1).replace('/', '\\');
		}
	}

	/** 获得自增长的文件名.
	 * @param fullfileName
	 * @return String
	 */
	public static String getIncrementFileName(String urlPath, String fullFileName) {
		if (fullFileName == null) {
			return null;
		}
		String fileName = getFileName(fullFileName);
		String fileExtend = getFileExtend(fullFileName);
		String filePath, s;
		boolean isSucceed = false;
		for (int i = 1; i < 1000; i++) {
			s = new Integer(i).toString();
			if (s.length() == 1) {
				s = "00" + s;
			}
			if (s.length() == 2) {
				s = "0" + s;
			}
			if (fileExtend != null) {
				fullFileName = fileName + s + "." + fileExtend;
			} else {
				fullFileName = fileName + s;
			}
			filePath = getFilePath(urlPath, fullFileName);
			File file = new File(filePath);
			if (!file.exists()) {
				isSucceed = true;
				break;
			}
		}
		if (isSucceed) {
			return fullFileName;
		} else {
			return null;
		}
	}
}
